package com.iot.broker.mvc.mqtt.handler;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.iot.broker.mvc.mqtt.bean.MQTTPayload;
import com.iot.common.core.domain.mqtt.Payload;
import com.iot.common.core.util.R;
import com.iot.manage.api.feign.RemoteUserService;
import com.iot.rule.api.feign.RemoteRuleService;
import com.jfinal.kit.JsonKit;

import lombok.RequiredArgsConstructor;


@RequiredArgsConstructor
@Component
public class SubscribeListener {
	
	private final RemoteRuleService remoteRuleService;
	
	@JmsListener(destination="sys.*.*.thing.property.report", containerFactory="topicListenerFactory")// 增加对应处理的监听器工程
	public void receiveProperty(TextMessage text,Session session) throws JMSException{
		
		try {
			//Payload payload = JSONObject.parseObject(text.getText(), Payload.class);
			
//			Payload payload=new Payload();
//			payload.setIdentifier("GGGG");
//			payload.setTimestamp(System.currentTimeMillis());
//			System.out.println(JsonKit.toJson(payload));
//			
//			R<Payload> r=remoteRuleService.handle(payload);
			System.out.println(text.getText());
			// 使用手动签收模式，需要手动的调用，如果不在catch中调用session.recover()消息只会在重启服务后重发
			text.acknowledge();
			session.commit();
			session.close();
		}catch (Exception e) {
			e.printStackTrace();
			session.recover();// 此不可省略 重发信息使用
		}

		//规则引擎
		// sys.*.*.thing.property.report_reply
	}
	
	@JmsListener(destination="sys.*.*.thing.event.report", containerFactory="topicListenerFactory")// 增加对应处理的监听器工程
	public void receiveEvent(TextMessage text) throws JMSException{
		System.out.println(text.getText());
	}
	
	@JmsListener(destination="sys.*.*.thing.service.report", containerFactory="topicListenerFactory")// 增加对应处理的监听器工程
	public void reportService(TextMessage text) throws JMSException{
		System.out.println(text.getText());
	}
	
	public static void main(String[] args) {
		Payload payload=new Payload();
		payload.setIdentifier("GGGG");
		payload.setTimestamp(System.currentTimeMillis());
		System.out.println(JsonKit.toJson(payload));
	}
	
	
}
